//
//  LPACtivityDetailDynamicTableViewCell.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 2017/6/9.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPACtivityDetailDynamicTableViewCell.h"
#import "ZJDynamicCommentTableViewCell.h"
#import "ZJDynamicLickTableViewCell.h"
#import "ZJDynamicSectonView.h"
#define Comment_CELLID @"ZJDynamicCommentTableViewCell"
#define Like_CELLID @"ZJDynamicLickTableViewCell"
#define SECTIONID @"ZJDynamicSectonView"
#import "LPActivityDetailTableFooterView.h"
#import "LPActivityDetailTableHeaderView.h"
#import "LPActivityDetailStudyNoDataTableViewCell.h"
@interface LPACtivityDetailDynamicTableViewCell ()<UITableViewDelegate,UITableViewDataSource,ZJDynamicSectonViewDelegate,LPActivityDetailFooterDelegate>
{
    ZJDyamicModel *_model;
    ///判断是不是cell点击的评论 YES表示是cell NO表示是sectionView上面的评论按钮
    BOOL isComment_Cell;
    ///发表评论需要的数据 (是sectionView上面的评论按钮的数据)
    ZJTalkModel *commentData_Talk;
    ///发表评论需要的数据 (是点击cell的数据)
    ZJCommentList *commentData_Comment;
}

/** tableview */
@property (nonatomic, strong) UITableView *tableView;
/**  数据  */
@property (strong, nonatomic) NSArray  *modelArray;
/**  tableHEIGTH  */
@property (assign, nonatomic) CGFloat  tableHeight;


@end
@implementation LPACtivityDetailDynamicTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self layoutUI];
    }
    return self;
}



- (void)setModel:(ZJDyamicModel *)model{
    _model = model;
    if (self.isDyamic) {
        self.tableHeight = 130 * [UIScreen zj_screenHScale];
    }else{
        self.tableHeight = 110 * [UIScreen zj_screenHScale] + model.dyamicCellHeight;
        LPActivityDetailTableFooterView *footer = [[LPActivityDetailTableFooterView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 60 * [UIScreen zj_screenHScale])];
        footer.titles = @[@"查看活动圈"];
        footer.delegate = self;
        self.tableView.tableFooterView = footer;
    }
    self.modelArray = model.CHILDREN_MODEL;
    CGRect frame = self.tableView.frame;
    frame.size.height = self.tableHeight;
    self.tableView.frame = frame;
    
    [self.tableView reloadData];
    
}

#pragma mark - 界面
- (void)layoutUI{
    [self addSubview:self.tableView];
    [self addFooterAndHeader];
}


#pragma mark - 私有方法
- (void)addFooterAndHeader{
    LPActivityDetailTableHeaderView *heaer = [[LPActivityDetailTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 50 * [UIScreen zj_screenHScale]) buttonTitle:@"动态" buttonImgae:@"wxy_dongtai_l"];
    self.tableView.tableHeaderView = heaer;
    
    
}

#pragma mark - LPActivityDetailFooterDelegate
- (void)lpActivityFooter_clickButton_index:(NSInteger)index{
    /**  查看更多的点击方法  */
    if (self.delegate) {
        [self.delegate lpActivityDetailDynamicWithCell:self watchMore:YES];
    }
}

#pragma mark - ZJDynamicSectonViewDelegate
- (void)ZJDynamicSectonViewDelegate_DeleteSuccess_index:(NSInteger)index{
    if (self.delegate) {
        [self.delegate lpActivityDetailDynamicWithCell:self DeleteIndex:index];
    }
}

- (void)ZJDynamicSectonViewDelegate_commentClick_index:(NSInteger)index{
    if (self.delegate) {
        [self.delegate lpActivityDetailDynamicWithCell:self sectionIndex:index sectionCommentTalk:self.modelArray[index]];
    }
}

- (void)ZJDynamicSectonViewDelegate_operationSuccess_index:(NSInteger)index{
    if (self.delegate) {
        [self.delegate lpActivityDetailDynamicWithCell:self spot:YES index:index];
    }
    
}

#pragma mark - UITableView 协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.isDyamic) {
        return 1;
    }else{
        return self.modelArray.count;
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.isDyamic) {
        return 1;
    }else{
        ZJTalkModel *model = self.modelArray[section];
        if (model.PARISELIST || model.PARISELIST.count > 0) {
            return  model.COMMENTLIST.count + 1;
        }
        return  model.COMMENTLIST.count;
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (self.isDyamic) {
        return nil;
    }else{
        ZJDynamicSectonView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:SECTIONID];
        view.model = self.modelArray[section];
        view.section = section;
        view.delegate = self;
        return view;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *weizhi = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    if (self.isDyamic) {
        LPActivityDetailStudyNoDataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
        if (!cell) {
            cell = [[LPActivityDetailStudyNoDataTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        }
        cell.title.text = @"暂无动态";
        return cell;
    }else{
        ZJTalkModel *model = self.modelArray[indexPath.section];
        if (indexPath.row == 0) {//是第一个
            if (model.PARISELIST && model.PARISELIST.count > 0) {//点赞存在
                ZJDynamicLickTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Like_CELLID forIndexPath:indexPath];
                if (model.COMMENTLIST.count > 0) {//有评论
                    cell.haveComment = YES;
                }else{
                    cell.haveComment = NO;
                }
                cell.likeArray = model.PARISELIST_MODEL;
                return cell;
            }else{//点赞不存在
                ZJDynamicCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Comment_CELLID forIndexPath:indexPath];
                cell.indexPath = indexPath;
                cell.talkModel = model;
                cell.model = model.COMMENTLIST_MODEL[indexPath.row];
                return cell;
            }
        }else{//不的第一个
            ZJDynamicCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Comment_CELLID forIndexPath:indexPath];
            cell.indexPath = indexPath;
            cell.talkModel = model;
            if (model.PARISELIST && model.PARISELIST.count > 0) {//点赞存在
                cell.model = model.COMMENTLIST_MODEL[indexPath.row - 1];
            }else{//点赞不存在
                cell.model = model.COMMENTLIST_MODEL[indexPath.row];
            }
            
            return cell;
        }
    }
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.isDyamic) {
        return 0;
    }else{
        ZJTalkModel *model = self.modelArray[section];
        
        return model.sectionViewHeight;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.isDyamic) {
        return 80 * [UIScreen zj_screenHScale];
    }else{
        ZJTalkModel *model = self.modelArray[indexPath.section];
        model.indexPath = indexPath;
        if (model.PARISELIST && model.PARISELIST.count > 0 && indexPath.row == 0) {//点赞cell
            return model.likeCellHeight;
        }else{
            return model.commentCellHeight;
        }
    }
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (!self.isDyamic) {
        ZJTalkModel *model = self.modelArray[indexPath.section];
        if ((model.PARISELIST || model.PARISELIST.count > 0) && indexPath.row == 0) {//点击的是点赞模块
            
        }else{
            if (model.PARISELIST || model.PARISELIST.count > 0) {//有点赞数据
                commentData_Comment = model.COMMENTLIST_MODEL[indexPath.row - 1];
                
            }else{
                commentData_Comment = model.COMMENTLIST_MODEL[indexPath.row];
                
            }
            if (self.modelArray.count != 0) {
                if (self.delegate) {
                    [self.delegate lpActivityDetailDynamicWithCell:self cellIndex:indexPath.section cellCommentData:commentData_Comment];
                }
                
            }
            
        }
    }
   
}

#pragma mark - Setter
#pragma mark - Getters
- (UITableView *)tableView{
    if (_tableView == nil){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], self.tableHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [_tableView registerClass:[ZJDynamicCommentTableViewCell class] forCellReuseIdentifier:Comment_CELLID];
        [_tableView registerClass:[ZJDynamicLickTableViewCell class] forCellReuseIdentifier:Like_CELLID];
        [_tableView registerClass:[ZJDynamicSectonView class] forHeaderFooterViewReuseIdentifier:SECTIONID];
    }
    return _tableView;
}

@end
